package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;

import com.atguigu.eduservice.entity.SystemOrganizationBase;
import com.atguigu.eduservice.entity.SystemOrganizationBase1;
import com.atguigu.eduservice.entity.excel.OrgExcel;
import com.atguigu.eduservice.listener.ExcelListener;
import com.atguigu.eduservice.mapper.OrgExcelMapper;
import com.atguigu.eduservice.mapper.SystemOrganizationBaseMapper;
import com.atguigu.eduservice.service.OrgExcelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class OrgExcelServiceImpl extends ServiceImpl<OrgExcelMapper, SystemOrganizationBase1> implements OrgExcelService {


    @Resource
    private SystemOrganizationBaseMapper systemOrganizationBaseMapper;

    @Override
    public void saveOrgExcel(MultipartFile file,OrgExcelService orgExcelService) {
        try {
            //文件输入流
            InputStream in = file.getInputStream();
            //调用方法进行读取
            EasyExcel.read(in, OrgExcel.class,new ExcelListener(orgExcelService)).sheet().doRead();
        }catch(Exception e){
            e.printStackTrace();
        }
    }



    @Override
    public Map getAllMap() {
        HashMap<Object, Object> objectObjectHashMap = Maps.newHashMap();

        return objectObjectHashMap;
    }

    private String getCode (String code) {
        return StringUtils.leftPad(code,3, "0");
    }




    @Override
    public List<SystemOrganizationBase1> getAll() {

        QueryWrapper<SystemOrganizationBase1> systemOrganizationBase1QueryWrapper = new QueryWrapper<>();
        systemOrganizationBase1QueryWrapper.eq("pid","0");
        //所有一级单位
        List<SystemOrganizationBase1> oneLevel = baseMapper.selectList(systemOrganizationBase1QueryWrapper);
        List<SystemOrganizationBase1> finalList = new ArrayList<>();

        int index = 1;
        for (int i = 0; i < oneLevel.size(); i++) {
            SystemOrganizationBase1 systemOrganizationBase1 = oneLevel.get(i);
            String oneCode = this.getCode(index++ + "");
            systemOrganizationBase1.setOrgCode(oneCode);
            systemOrganizationBase1.setParentCode("0");
            baseMapper.updateById(systemOrganizationBase1);
            finalList.add(systemOrganizationBase1);
            List<SystemOrganizationBase1> xiaji = this.xiaji(systemOrganizationBase1.getOrganizationBaseId());
            int twoIndex = 1;
            for (int j = 0; j < xiaji.size() ; j++) {
                SystemOrganizationBase1 systemOrganizationBase11 = xiaji.get(j);
                String twoCode = oneCode + this.getCode(twoIndex++ + "");
                systemOrganizationBase11.setParentCode(oneCode);
                systemOrganizationBase11.setOrgCode(twoCode);
                baseMapper.updateById(systemOrganizationBase11);
                finalList.add(systemOrganizationBase11);
                List<SystemOrganizationBase1> xiaji1 = this.xiaji(systemOrganizationBase11.getOrganizationBaseId());
                int threeIndex =1;
                for (int k = 0; k < xiaji1.size(); k++) {
                    SystemOrganizationBase1 systemOrganizationBase12 = xiaji1.get(k);
                    String threeCode = twoCode + this.getCode(threeIndex++ + "");
                    systemOrganizationBase12.setOrgCode(threeCode);
                    systemOrganizationBase12.setParentCode(twoCode);
                    baseMapper.updateById(systemOrganizationBase12);
                    finalList.add(systemOrganizationBase12);
                }
            }

        }
      return finalList;

    }

    //根据pid查询出所有下级单位
    public List<SystemOrganizationBase1> xiaji(String id){
        QueryWrapper<SystemOrganizationBase1> systemOrganizationBase1QueryWrapper = new QueryWrapper<>();
        systemOrganizationBase1QueryWrapper.eq("pid",id);
        List<SystemOrganizationBase1> systemOrganizationBase1s = baseMapper.selectList(systemOrganizationBase1QueryWrapper);
        return systemOrganizationBase1s;


    }



    @Override
    public void updOrIns() {
        List<SystemOrganizationBase> systemOrganizationBases = systemOrganizationBaseMapper.selectList(null);
        List<SystemOrganizationBase1> systemOrganizationBase1s = baseMapper.selectList(null);


        for (int i = 0; i <systemOrganizationBase1s.size() ; i++) {
            SystemOrganizationBase1 newBase = systemOrganizationBase1s.get(i);


            for (int j = 0; j < systemOrganizationBases.size(); j++) {
                SystemOrganizationBase oldBase = systemOrganizationBases.get(j);

                if (oldBase.getOrganizationName().equals(newBase.getOrganizationName())){

                    BeanUtils.copyProperties(oldBase,newBase);
                    baseMapper.updateByName(newBase);

                }

            }

        }


    }





}
